<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class supply extends CI_Controller {

    function __construct(){
        parent::__construct();
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("If-Modified-Since: Mon, 22 Jan 2008 00:00:00 GMT");
		header("Cache-Control: no-store, no-cache, must-revalidate");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Cache-Control: private");
		header("Pragma: no-cache");
		$this->auth	= unserialize(base64_decode($this->session->userdata('sisfodealer')));
		$this->load->model(array('msupply','mdealer'));
	}

	public function index(){
		if($this->auth){
			$data['module'] = "supply";
			$this->load->view('home', $data);
		}else{
			$data['pesan'] = "<font color='red'>anda harus login!</font>";
			$this->load->view('login',$data);
			return;
		}
	}
	
	function getdisplay($param=""){
		$data['type'] = $param;
		$this->load->view('supply/supply_mng', $data);
	}
	
	function gettab(){
		$type = $this->input->post('type');
		switch($type){
			case 'wilayah':
			break;
		}
		$data['type'] = $type;
		$this->load->view('supply/supply_tab',$data);
	}
	
	function getdata($param=""){
		echo $this->msupply->getdata($param);
	}
	
	function savedata($type){
		$post = array();
        foreach($_POST as $k=>$v) $post[$k] = $this->input->post($k);
		//print_r($post);
		//exit;
		
		$editstatus = $post['editstatus'];
		unset($post['editstatus']);
		if($editstatus == 'add'){
			unset($post['id']);
		}
		
		echo $this->msupply->savedatasupply($post,$type,$editstatus);
	}	
	
	function getform($editstatus="", $type=""){		
		if($editstatus == 'edit'){	
			$id = $this->input->post('id');
			$datarecord = $this->msupply->getrecord($type, $id);
			$data['datarecord'] = $datarecord;
			if($type == 'pesanan_konsumen'){
				$data['combo_jenis_penjualan'] = $this->combobox('jenis_penjualan', $datarecord->jenis_penjualan, 'return');
			}elseif($type == 'mutasi_generator' || $type == 'mutasi_promosi'){
				$data['detailmutasi'] = $this->msupply->getdata('detail_mutasi', $datarecord->no_mutasi_barang);
			}elseif($type == 'penerimaan_generator' || $type == 'penerimaan_promosi'){
				$data['detailpenerimaan'] = $this->msupply->getdata('detail_penerimaan', $datarecord->no_penerimaan_barang);
			}
		}elseif($editstatus == 'add'){
			if($type == 'pesanan_konsumen'){
				$data['combo_jenis_penjualan'] = $this->combobox('jenis_penjualan','','return');
				$data['no_surat_pesanan'] = "PSN-".$this->mdealer->randomString(3,'no_surat')."-".$this->mdealer->randomString(4,'no_surat');
			}elseif($type == 'mutasi_generator' || $type == 'mutasi_promosi'){
				$data['no_mutasi_barang'] = "MTS-".$this->mdealer->randomString(3,'no_surat')."-".$this->mdealer->randomString(4,'no_surat');
			}elseif($type == 'penerimaan_generator' || $type == 'penerimaan_promosi'){
				$data['no_penerimaan_barang'] = "PNR-".$this->mdealer->randomString(3,'no_surat')."-".$this->mdealer->randomString(4,'no_surat');
			}
		}
		
		$data['tinggiregion'] 	= $this->input->post('tinggiregion');
		$data['editstatus'] 	= $editstatus;
		$data['type']			= $type;
		$this->load->view('supply/supply_form', $data);
	}
	
	
	function combobox($type, $param="", $balikan=''){
		$optTemp = "";
		if($param != ""){
			$p2 = $param;
		}else{
			$p2 = "";
		}
						
		if($type == 'jenis_penjualan'){
			$data = array(
				'0' => array(
					'code' => 'T',
					'value' => 'Tunai'
				),
				'1' => array(
					'code' => 'KL',
					'value' => 'Kredit Leasing'
				),
				'2' => array(
					'code' => 'KD',
					'value' => 'Kredit Dealer'
				),
			);
		}elseif($type == 'tanggal'){
		
		}else{
			$data = $this->msupply->getcombo($type, $param);
		}
		
		$optTemp .= "<option value=''>-- Pilih --</option>";			
		foreach($data as $v=>$k){
			if($p2 == $k['code']){
				$optTemp .= "<option selected value='".$k['code']."'>".$k['value']."</option>";
			}else{
				$optTemp .= "<option value='".$k['code']."'>".$k['value']."</option>";
			}
		}
		
		if($balikan == 'echo'){
			echo $optTemp;
		}elseif($balikan == 'return'){
			return $optTemp;
		}
		
	}
	
	
}